﻿using System;
using System.Collections.Generic;
using System.IO;
using LuaInterface;
using System.Windows.Forms;
using System.Text;

namespace Calendar.Domain
{
    public delegate void AddWorkHandler(int id, string name, DateTime deadline);
    public delegate void AddDateWorkHandler(int id, string name, DateTime deadline);
    public delegate void EmptyWorkHandler();

    public class TodoList
    {
        public AddWorkHandler AddWorkEvent = null;
        public AddDateWorkHandler AddDateWorkEvent = null;
        public EmptyWorkHandler EmptyWorkEvent = null;
        Dictionary<int, Work> _workDictionary;
        int _workID;
        FileIO _file;

        public TodoList()
        {
            _workDictionary = new Dictionary<int, Work>();
            _workID = 1;
            _file = new FileIO();
        }

        public void AddWork(Work work)
        {
            AddWork(work.Name, work.StartTime, work.Deadline, work.EmergencyLevel, work.Progress);
        }

        public void AddWork(string workName, DateTime startDateTime, DateTime deadlineDateTime, int emergancyLevel, int progress)
        {
            Work work = new Work(workName, startDateTime, deadlineDateTime, emergancyLevel, progress);

            if(!isDeadlineWorkExist(deadlineDateTime, workName))
            {
                if (isDeadlineExist(deadlineDateTime))
                {    
                    _workDictionary.Add(_workID, work);               
                    AddWorkEvent.Invoke(_workID, workName, deadlineDateTime);
                }
                else
                {
                    _workDictionary.Add(_workID, work); 
                    AddDateWorkEvent.Invoke(_workID, workName, deadlineDateTime);
                }
                _workID++;
            }
        }

        public void LoadWork()
        {
            List<Work> workList = _file.LoadWork();
            if(workList.Count > 0)
            {
                foreach (Work work in workList)
                {
                    AddWork(work);
                }
            }
            else
            {
                EmptyWorkEvent.Invoke();
            }
        }

        public void SaveWork()
        {
            _file.SaveWork(_workDictionary);
        }

        public void DeleteWork(int id)
        {
            _workDictionary.Remove(id);
        }

        private bool isDeadlineExist(DateTime deadline)
        {
            foreach (Work work in _workDictionary.Values)
            {
                if (work.Deadline.Date == deadline.Date)
                {
                    return true;
                }
            }
            return false;
        }

        private bool isDeadlineWorkExist(DateTime deadline, string workName)
        {
            foreach (Work work in _workDictionary.Values)
            {
                if (work.Deadline.Date == deadline.Date && work.Name == workName)
                {
                    return true;
                }
            }
            return false;
        }

        public string GetName(int id)
        {
            return _workDictionary[id].Name;
        }

        public DateTime GetStartTime(int id)
        {
            return _workDictionary[id].StartTime;
        }

        public DateTime GetDeadline(int id)
        {
            return _workDictionary[id].Deadline;
        }

        public int GetEmergencyLevel(int id)
        {
            return _workDictionary[id].EmergencyLevel;
        }

        public int GetProgress(int id)
        {
            return _workDictionary[id].Progress;
        }

        //public void SetFullProgress(int id)
        //{
        //    if (_workDictionary[id].Progress < 100)
        //    {
        //        _workDictionary[id].Progress = 100;
        //    }
        //}

        internal void SetName(int id, string name)
        {
            _workDictionary[id].Name = name;
        }

        internal void SetStartTime(int id, DateTime startTime)
        {
            _workDictionary[id].StartTime = startTime;
        }

        internal void SetDeadline(int id, DateTime deadline)
        {
            _workDictionary[id].Deadline = deadline;
        }

        internal void SetEmergancyLevel(int id, int emergancyLevel)
        {
            _workDictionary[id].EmergencyLevel = emergancyLevel;
        }

        public void SetProgress(int id, int persentage)
        {
            _workDictionary[id].Progress = persentage;
        }
    }
}
